/*
 *   Copyright 2012, Thomas Kerber
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package milk.jpatch.access;

import milk.jpatch.CPoolMap;
import milk.jpatch.Patch;

/**
 * Denotes a patch in the access flags.
 * @author Thomas Kerber
 * @version 1.0.0
 */
public abstract class AccessFlagsPatch implements Patch<Integer>{
    
    /**
     * The possible levels on which the access flags can occur:
     * class, method, field.
     * 
     * They vary slightly between these levels, but are mostly the same.
     * @author Thomas Kerber
     * @version 1.0.0
     */
    public static enum AccessLevel{
        CLASS,
        METHOD,
        FIELD
    };
    
    private static final long serialVersionUID = -7243618468091967388L;
    
    /**
     * Generates a new access flags patch.
     * @param old The old access flags.
     * @param new_ The new access flags.
     * @param a The access level of the flags.
     * @return The patch to get from the old flags to the new.
     */
    public static AccessFlagsPatch generate(int old, int new_, AccessLevel a){
        if(old == new_)
            return new AccessFlagsID();
        return new AccessFlagsModif(
                AccessPatch.generate(old, new_, a),
                RestFlagsPatch.generate(old, new_, a),
                a);
    }
    
    /**
     * Convenience method to use int and ignore the CPoolMap.
     * @param flags The flags to patch.
     * @return The patched flags.
     */
    public abstract int patch(int flags);
    
    @Override
    public final Integer patch(Integer flags, CPoolMap map){
        if(flags == null)
            return null;
        return patch(flags.intValue());
    }
    
}
